﻿using System.Collections.Generic;
using HRC.Library.ContextFoundation;
using HRC.Library.DBAccessLayer.Parameters;
using HRC.Library.EntityLibrary.EntityOperations;
using Lehnet.BusinessLayer.Interfaces;

namespace Lehnet.BusinessLayer
{
    public class MusteriBusiness : IMusteriBusiness
    {
        public List<Models.Musteri> LoadAll()
        {
            return EntityManager.LoadAll<Models.Musteri>();
        }

        public void SaveOrUpdate(Models.Musteri musteri)
        {
            if (musteri.Id == 0)
                EntityManager.Insert(musteri);
            else if (musteri.Id > 0)
            {
                musteri = EntitySessionManager.Instance.GetOldEntity<Models.Musteri>(musteri);
                ProxyHelper<MusteriBusiness, IMusteriBusiness>.Instance.AddOrGet().Update(musteri);
            }
        }

        public void Delete(int musteriId)
        {
            EntityManager.Delete<Models.Musteri>(new HRCParameter("Id", musteriId, HRCParameterType.INT));
            //Müşsteri silindiği zaman müşteriye bağlı kayıtların da silinmesi
            EntityManager.Delete<Models.MusteriPersonel>(new HRCParameter("MusteriId", musteriId, HRCParameterType.INT));
            EntityManager.Delete<Models.MusteriAdres>(new HRCParameter("MusteriId", musteriId, HRCParameterType.INT));
        }

        public Models.Musteri LoadMusteriById(int Id)
        {
            return EntityManager.Load<Models.Musteri>(" where Id=" + Id.ToString());
        }


        public void Update(Models.Musteri musteri)
        {
            EntityManager.Update(musteri);
        }
    }
}
